package cn.jiyun.controller;

//import cn.jiyun.common.AutoLogs;
import cn.jiyun.common.config.Result;
import cn.jiyun.pojo.Role;
import cn.jiyun.service.RoleService;
import cn.jiyun.service.impl.RoleServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("role")
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleServiceImpl roleServiceImpl;

    @RequestMapping("/findPage")
    public Result findPage(Integer pageNum, Integer pageSize, @RequestBody Role role){
        Page<Role> page=roleService.findPage(pageNum,pageSize,role);

        return  Result.ok(page);
    }

    @RequestMapping("/add")
//    @AutoLogs(value = "角色权限添加操作", loggingtype  = 0)
    public Result add(@RequestBody Role role){
        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper<>();
        qw.eq(Role::getName,role.getName());
        Role one = roleService.getOne(qw);
        if (one!=null){
            return Result.fail("角色名已存在！");
        }
        if(role.getId()==null){
            role.setTime(LocalDateTime.now());
            role.setTongji(0);
            roleService.save(role);
        }else{
            roleService.updateById(role);
        }
        return Result.ok("添加成功！");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role=roleService.getById(id);
        return Result.ok(role);
    }

    @RequestMapping("/deleteById")
//    @AutoLogs(value = "角色权限删除操作", loggingtype  = 0)
    public Result deleteById(Integer id){
        try {
            roleService.removeById(id);
            return Result.ok("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败！");
    }
    @RequestMapping("chakan")
    public Result chakan(Integer id){
        Role role =  roleService.chakan(id);
        return Result.ok(role);
    }

}
